#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSqlDatabase>
#include <QDebug>
#include <QMessageBox>
#include <QSqlQuery>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    qDebug() << QSqlDatabase::drivers();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::connectDB()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "myConnection");
    db.setDatabaseName("mydb");
    bool r = db.open();
    if(r == true){
        QMessageBox::information(0, "通知", "恭喜，数据库连接成功");
    }else{
        QMessageBox::critical(0, "警告", "注意，数据库连接失败");
    }
}

void MainWindow::createTable()
{
    QSqlDatabase db = QSqlDatabase::database("myConnection");
    QSqlQuery sqlQuery(db);
    bool r = sqlQuery.exec("create table student(id integer, name verchar, score integer)");
    qDebug() << r;

}

void MainWindow::on_pushButton_clicked()
{
    connectDB();
}

void MainWindow::on_pushButton_2_clicked()
{
    createTable();
}
